package org.ssp.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.ssp.base.ResultSetHanlder;
import org.ssp.dao.PartJobDao;
import org.ssp.pojo.PartJob;
import org.ssp.util.DBUtil;

public class PartJobImpl implements PartJobDao{

	@Override
	public void add(PartJob partjob) {
		String sql="insert into partjob(title,content,datetime) values(?,?,curdate());";
		DBUtil.update(sql, partjob.getTitle(),partjob.getContent());
	}
	@Override
	public ArrayList<PartJob> findAll(int offset, int rows) {
		String sql="select * from partjob limit ?,?;";
		return (ArrayList<PartJob>)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<PartJob> list=new ArrayList<PartJob>();
				try {
					while(rs.next()){
						PartJob partjob=new PartJob();
						partjob.setId(rs.getInt("id"));
						partjob.setTitle(rs.getString("title"));
						partjob.setContent(rs.getString("content"));
						partjob.setDate(rs.getDate("datetime"));
						list.add(partjob);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return list;
			}
		}, offset,rows);
	}
	@Override
	public int findAll() {
		String sql="select * from partjob;";
		return (Integer)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				Integer total=0;
				try {
					while(rs.next()){
						total+=1;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return total;
			}
		}, null);
	}
	@Override
	public PartJob findById(int id) {
		return null;
	
	}
	@Override
	public void update(PartJob partjob) {
		String sql="update partjob set title=?,content=? where id=?";
		DBUtil.update(sql, partjob.getTitle(),partjob.getContent(),partjob.getId());
	}
	@Override
	public void delete(int id) {
		String sql="delete from partjob where id=?;";
		DBUtil.update(sql, id);
	}
	@Override
	public ArrayList<PartJob> getJobs() {
		String sql="select * from partjob;";
		return (ArrayList<PartJob>)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<PartJob> list=new ArrayList<PartJob>();
				try {
					while(rs.next()){
						PartJob pjob=new PartJob();
						pjob.setId(rs.getInt("id"));
						pjob.setTitle(rs.getString("title"));
						pjob.setContent(rs.getString("content"));
						pjob.setDate(rs.getDate("datetime"));
						list.add(pjob);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return list;
			}
		}, null);
		
	}
	@Override
	public String getContent(int id) {
		String sql="select * from partjob where id=?";
		return (String)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				String content=new String();
				try {
					if(rs.next()){
						content=rs.getString("content");
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return content;
			}
		}, id);
	}

}
